﻿using BZ.Logger;
using HalconDotNet;
using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.MachineManager;
using NDK.Motion.Monitor;
using NDK.Motion.Sys;
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Windows.Forms;

namespace NDK.Module.StationLogic
{
    public class Station_转盘五穴 : StationLogic
    {
        #region 通用
        public override event PropertyChangedEventHandler PropertyChanged;

        public static Station_转盘五穴 Instance = new Station_转盘五穴();
        public Stopwatch SunWay05 = new Stopwatch();
        public string StationName = "Station05转盘5工位";

        //步骤字段、属性
        private StationStep _Step = StationStep.扫描轴运行至起始位;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _Step;
            }
            set
            {
                if(_Step != value)
                {
                    if (!Common.BP_是否屏蔽计时)
                    {
                        WriteStationCTLog("StationCTLog", $"{_Step}用时为：{swStepTime.ElapsedMilliseconds} ms");
                        swStepTime.Restart();
                    }
                    _Step = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        Stopwatch ct;
        #endregion
        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init ()
        {
            base.Init();
            StationID = 5;
            CurHoleId = 5;
            logBaseInfo = "转盘五穴流程";
            Step = StationStep.扫描轴运行至起始位;
            QueueImage = new ConcurrentQueue<MeasureImage>();
        }

        public override void ActionProcess ()   
        {
            try
            {
                switch(Step)
                {
                    case StationStep.扫描轴运行至起始位:
                        if(MotionControl.AxesMove(Machine.pos_激光扫描起始位_转盘工位五))
                        {
                            Step = StationStep.判断是否工作;
                        }
                        break;

                    case StationStep.判断是否工作:
                        if(!Common.AllStationCanWork)
                            break;
                        if(!Common.DicStationWorkStatus[StationID])
                        {
                            Step = Common.DicHoleCanWork[CurHoleId] ? StationStep.触发激光开始扫描 : StationStep.工位流程完成;
                            SunWay05.Restart();
                        }
                        break;

                    case StationStep.触发激光开始扫描:
                        if(!Common.IsEmptyModel)
                        {
                            if (Common.Lmi_短边前激光.Start() && Common.Lmi_短边后激光.Start())
                            {

                            }
                            else
                            {
                                System.Windows.MessageBox.Show("五工站激光打开失败");
                                MachineStatusManager.CurrentMachineStatus = MachineStatus.Alarm;
                                break;
                            }
                          
                        }
                        Step = StationStep.运行至激光扫描终点位;
                        break;

                    case StationStep.运行至激光扫描终点位:
                        if(MotionControl.AxesMove(Machine.pos_激光扫描终点位_转盘工位五))
                        {
                            Step = Common.IsEmptyModel ? StationStep.工位流程完成 : StationStep.获取激光扫描图片;
                        }
                        break;

                    case StationStep.获取激光扫描图片:
                        var image = Common.Lmi_短边前激光.HeightImage();
                        var image1 = Common.Lmi_短边后激光.HeightImage();
                        Common.Lmi_短边前激光.Stop();
                        Common.Lmi_短边后激光.Stop();
                        if(image != null && image1 != null)
                        {
                            QueueImage.Enqueue(new MeasureImage() { Hole = CurHoleId, Images = new (string, HImage)[2] { ("短边1", image), ("短边2", image1) } });
                            Step = StationStep.工位流程完成;
                        }
                        else
                        {
                            Common.Lmi_短边前激光.Connect(Common.SP_工位5_LMI短边前激光IP);
                            Common.Lmi_短边后激光.Connect(Common.SP_工位5_LMI短边后激光IP);
                            Step = StationStep.扫描轴运行至起始位;
                            //AlarmPause("激光出图异常", "工位五激光出图异常", "请检查", "重试");
                            if (AlarmPause("激光出图异常", $"五工站激光出图异常，请检查", "重新检测", "忽略报警，继续运行") == DialogResult.OK)
                            {
                                break;
                            }
                        }
                        break;

                    case StationStep.工位流程完成:
                        CurHoleId++;
                        if(CurHoleId > 8)
                        {
                            CurHoleId = 1;
                        }
                        Common.ChangeTurnTableStatus(StationID, true);
                        Step = StationStep.扫描轴运行至起始位;
                        break;

                    default:
                        break;
                }
            }
            catch(Exception ex)
            {
                UILog_Error($"[{logBaseInfo}] {ex.Message}");
            }
        }

        public override void EmptyActionProcess ()
        {
            ActionProcess();
        }

        public override void GRRActionProcess ()
        {
            ActionProcess();
        }

        public enum StationStep
        {
            扫描轴运行至起始位,
            判断是否工作,
            触发激光开始扫描,
            运行至激光扫描终点位,
            等待激光扫描完成,
            获取激光扫描图片,
            工位流程完成
        }
    }

}
